/*

Frequency of use: 1 2 3 4 [5] HIGH

Short:
    Iterator provides a place-holder into a collection.  In this example, 
StringIterator provides a placeholder into a string.  Note that C++ iterators 
are similar but have a different interface.

Long:
    In object-oriented programming, the Iterator pattern is a design pattern in 
which iterators are used to access the elements of an aggregate object 
sequentially without exposing its underlying representation. An Iterator object 
encapsulates the internal structure of how the iteration occurs.
For example, a tree, linked list, hash table, and an array all need to be 
iterated with the methods search, sort, and next. Rather than having 12 
different methods to manage (one implementation for each of the previous three 
methods in each structure), using the iterator pattern yields just seven: one 
for each class using the iterator to obtain the iterator and one for each of the 
three methods. Therefore, to run the search method on the array, you would call 
array.search(), which hides the call to array.iterator.search().

Information sources:
 - http://calumgrant.net/patterns/index.html
 - http://en.wikipedia.org/wiki/Iterator_pattern
 - http://www.dofactory.com/Patterns/Patterns.aspx
*/

#include <iostream>
#include <cstring>

class StringIterator
{
	const char *position, *end;
public:
	StringIterator(const char * str) : 
		position(str), 
		end(str + strlen(str))  { }
	bool at_end() const { return position == end; }
	void advance() { ++position; }
	char get_char() const { return *position; }
};

void hello_world(StringIterator & iterator)
{
	for(StringIterator i=iterator; !i.at_end(); i.advance())
	{
		std::cout << i.get_char();
	}
}

int main()
{
	StringIterator iterator("Hello world!\n");
	hello_world(iterator);
	return 0;
}

